<?php

	$link=mysql_connect('db.cs.dal.ca', 'tameem', 'B00614969') or die("could not connect");
	mysql_select_db('tameem') or die ("could not access db");
	
//	$Publish_List = array();//need to figure out this received data

//	foreach($Publish_List as $publish_target)
//	{
//		$query = "select Template, Output_Name, Type from Template_List where Unique_ID like '".$publish_target."'";
//		$result = mysql_query($query) or die("Querry Failed");

//		while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
//		{	
//			$field_num = 0;
//			$paramter_array = array();//array([0] => 0 [1] => 0 [2] => 0 [3] => 0);
			
//			foreach($line as $col_value)
//			{
				//0 = unique ID
				//1 = template
				//2 = output name
				//3 = ouput type
//				$paramter_array[$field_num] = $col_value;
				
//				$field_num++;
//			}
			
			$paramter_array[1] = "testinput1.txt";
			$paramter_array[2] = "testoutput1";
			
			$outfile = fopen($paramter_array[2].".txt", "w") or die ("could not open output file");

			//<<-- file extender start
			$mySQL_mode = 0;
			$Template_Text_Array = file($paramter_array[1]) or die ("could not open input file");
			$mySQL_String_CMD = "";
			foreach($Template_Text_Array as $line)
			{
				$Trimmed_Line = trim($line);
				if($mySQL_mode == 0 && $Trimmed_Line == '<!--sql')
				{
					echo "mySQL start tag found\n";
					$mySQL_mode = 1;
					$mySQL_String_CMD = "";
					continue;
				}
				elseif($mySQL_mode == 1 && $Trimmed_Line == 'sql-->')
				{
					echo "mySQL end tag found\n";
					$mySQL_mode = 0;
					
					$DatabasePoll = mysql_query($mySQL_String_CMD) or die("Line Querry Failed");
					echo $mySQL_String_CMD."\n";
					while ($db_line = mysql_fetch_array($DatabasePoll, MYSQL_ASSOC))
					{
						$line_to_file = "";
						foreach($db_line as $db_field)
						{
							$line_to_file = $line_to_file.$db_field.", ";
						}
						$line_to_file = $line_to_file."\n";
						fwrite($outfile, $line_to_file);
					}
					continue;
				}
				
				if($mySQL_mode == 1)
				{
					$mySQL_String_CMD = $mySQL_String_CMD." ".$Trimmed_Line;
				}
				else
				{
					fwrite($outfile, $line);
				}
			}
			fclose($outfile);
			echo "file closed\n";
			//<<-- file extender end
			
//			if($paramter_array[3] == 'pdf' || $paramter_array[3] == 'latex')
//			{
//				shell_exec("latex ".$paramter_array[2].".DAT");
//				shell_exec("latex ".$paramter_array[2].".DAT");
//				shell_exec("dvips -P cmz -t letter ".$paramter_array[2].".dvi");
//				
//				//final publish for pdf and latex
//				
//				shell_exec("move .\\".$paramter_array[2].".".$paramter_array[3]." ..\\public\\"$paramter_array[2].".".$paramter_array[3]);
//			}
//			else
//			{
//				shell_exec("move .\\".$paramter_array[2].".DAT" ..\\public\\"$paramter_array[2].".".$paramter_array[3]);
//			}
			
//			shell_exec("del *.dvi");
//			shell_exec("del *.aux");
//			shell_exec("del *.log");
//		}
//	}
?>
